package com.huanzhidadi.single.listmapfind;

import org.springframework.util.StopWatch;

import java.util.*;

public class ListMapFind {

    public static void main(String[] args) {
        // linkedListTest();
        arrayListTest();
        // hashMapTest();
    }

    private static void hashMapTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("创建Map");
        Map<String, Hero> heroMap = new HashMap<>();
        for (int i=0; i<200*10000; i++) {
            Hero hero = new Hero("Hero " + i);
            heroMap.put(hero.getName(), hero);
        }
        stopWatch.stop();
        stopWatch.start("目标查找");
        for (int i=0; i<100; i++) {
            long start = System.currentTimeMillis();
            Random random = new Random();
            String target = "Hero " + random.nextInt(2000000);
            System.out.println("target:" + target);
            Hero hero = heroMap.get(target);
            System.out.println("找到了Hero：" + hero.getName());
            long end = System.currentTimeMillis();
            System.out.println("第" + (i+1) + "次查询花了：" + (end-start) + " 毫秒");
        }
        stopWatch.stop();
        System.out.println("总用时：" + stopWatch.getTotalTimeSeconds() + "秒");
    }

    private static void linkedListTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("创建队列");
        List<Hero> heroList = new LinkedList<>();
        for (int i=0; i<200*10000; i++) {
            Hero hero = new Hero("Hero " + i);
            heroList.add(hero);
        }
        stopWatch.stop();
        stopWatch.start("队列洗牌");
        Collections.shuffle(heroList);
        stopWatch.stop();
        stopWatch.start("目标查找");
        for (int i=0; i<100; i++) {
            long start = System.currentTimeMillis();
            Random random = new Random();
            String target = "Hero " + random.nextInt(2000000);
            System.out.println("target:" + target);
            for (Hero hero : heroList) {
                if (hero.getName().equals(target)) {
                    System.out.println("找到了Hero：" + hero.getName());
                    break;
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("第" + (i+1) + "次查询花了：" + (end-start) + " 毫秒");
        }
        stopWatch.stop();
        System.out.println("总用时：" + stopWatch.getTotalTimeSeconds() + "秒");
    }

    private static void arrayListTest() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("创建队列");
        List<Hero> heroList = new ArrayList<>();
        for (int i=0; i<200*10000; i++) {
            Hero hero = new Hero("Hero " + i);
            heroList.add(hero);
        }
        stopWatch.stop();
        stopWatch.start("队列洗牌");
        Collections.shuffle(heroList);
        stopWatch.stop();
        stopWatch.start("目标查找");
        for (int i=0; i<100; i++) {
            long start = System.currentTimeMillis();
            Random random = new Random();
            String target = "Hero " + random.nextInt(2000000);
            System.out.println("target:" + target);
            for (Hero hero : heroList) {
                if (hero.getName().equals(target)) {
                    System.out.println("找到了Hero：" + hero.getName());
                    break;
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("第" + (i+1) + "次查询花了：" + (end-start) + " 毫秒");
        }
        stopWatch.stop();
        System.out.println("总用时：" + stopWatch.getTotalTimeSeconds() + "秒");
    }
}
